<?php

namespace App\Admin\Controllers\Mp;

use App\Admin\Repositories\Mp\WxMpMessage;
use App\Lib\WeApp\WeApp;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxMpMessageController extends AdminController
{
    protected $mp_message_types = [
        'text' => '文本',
        'image' => '图片',
        'voice' => '语音',
        'video' => '视频',
        'music' => '音乐',
        'news' => '外链图文',
        'mpnews' => '公众号图文',
        'wxcard' => '卡卷消息'
    ];
    protected $mp_message_type_colors = [
        'text' => '#3498db',
        'image' => '#e74c3c',
        'voice' => '#2ecc71',
        'video' => '#f1c40f',
        'music' => '#9b59b6',
        'news' => '#e67e22',
        'mpnews' => '#1abc9c',
        'wxcard' => '#34495e'
    ];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxMpMessage(), function (Grid $grid) {
            $grid->model()->whereNotIn('msgtype',['material_image', 'material_video', 'material_voice', 'material_music'])->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('msgtype')->using($this->mp_message_types)->label($this->mp_message_type_colors);
            $grid->column('content')->limit(100);
            $grid->column('img_url')->image('', 100, 100);
            $grid->column('video_url')->display(function ($v){
                if($v){
                    return '<video controls width="150">
  <source src="'.$v.'" type="video/mp4">
  您的浏览器不支持 video 标签。
</video>';
                }
                return '';
            });
            $grid->column('voice_url')->display(function ($v){
                if($v){
                    return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
                }
                return '';
            });
            $grid->column('thumb_url')->image('', 100, 100);
            $grid->column('musicurl')->display(function ($v){
                if($v){
                    return '<audio controls><source src="'.$v.'" type="audio/mp3"></audio>';
                }
                return '';
            });
//            $grid->column('media_id')->display();
//            $grid->column('thumb_media_id');
            $grid->column('title');
//            $grid->column('description');
//            $grid->column('hqmusicurl');
            $grid->column('url')->display(function ($v){
                if($v){
                    return '<a href="'.$v.'" target="_blank">链接</a>';
                }
                return '';
            });
//            $grid->column('article_id');
//            $grid->column('card_id');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxMpMessage(), function (Show $show) {
            $show->field('id');
            $show->field('msgtype');
            $show->field('content');
            $show->field('img_url');
            $show->field('video_url');
            $show->field('voice_url');
            $show->field('thumb_url');
            $show->field('musicurl');
            $show->field('media_id');
            $show->field('thumb_media_id');
            $show->field('title');
            $show->field('description');
            $show->field('hqmusicurl');
            $show->field('url');
            $show->field('article_id');
            $show->field('card_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxMpMessage(), function (Form $form) {
            $form->display('id');
            // 'text' => '文本',
            // 'image' => '图片',
            // 'voice' => '语音',
            // 'video' => '视频',
            // 'music' => '音乐',
            // 'news' => '外链图文',
            // 'mpnews' => '公众号图文',
            // 'wxcard' => '卡卷消息'
            $form->radio('msgtype')->options($this->mp_message_types)->default('text')
                ->when(['video', 'news', 'music'], function (Form $form){
                $form->text('title')->rules('required_if:msgtype,video,news,music') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;
                $form->textarea('description')->rules('required_if:msgtype,video,news,music') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;
            })->when(['text'], function (Form $form){
                $form->textarea('content')->rules('required_if:msgtype,text') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;
            })->when(['image'], function (Form $form){
                $form->image('img_url')->rules('required_if:msgtype,image') // 使用required_if
                ->setLabelClass(['asterisk']) // 显示 * 号;
                ->uniqueName()->url('files/uploads')->autoUpload();
            })->when(['voice'], function (Form $form){
                $form->file('voice_url')->maxSize(2048)->accept('mp3')->rules('required_if:msgtype,voice') // 使用required_if
                ->setLabelClass(['asterisk']) // 显示 * 号;
                ->uniqueName()->url('files/uploads')->autoUpload();
            })->when(['video'], function (Form $form){
                $form->file('video_url')->maxSize(10240)->accept('mp4')->rules('required_if:msgtype,video') // 使用required_if
                ->setLabelClass(['asterisk']) // 显示 * 号;
                ->uniqueName()->url('files/uploads')->autoUpload();
            })->when(['music'], function (Form $form){
                $form->file('musicurl')->accept('mp3')->rules('required_if:msgtype,music') // 使用required_if
                ->setLabelClass(['asterisk']) // 显示 * 号;
                ->uniqueName()->url('files/uploads')->autoUpload();
            })->when(['video', 'news', 'music'], function (Form $form){
                $form->image('thumb_url')->rules('required_if:msgtype,video,news,music') // 使用required_if
                ->setLabelClass(['asterisk']) // 显示 * 号;
                ->uniqueName()->url('files/uploads')->autoUpload();
            })->when(['news'], function (Form $form){
                $form->text('url')->rules('required_if:msgtype,news') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;
            })->when(['mpnews'], function (Form $form){
                    $article_ids = [];
                    $weapp = new WeApp('mp');
                    if($weapp){
                        $mp = $weapp->getMpServicer();
                        if($mp){
                            $publishs = $mp->getFreepublishBatchget();
                            if($publishs['code'] == 200){
                                foreach ($publishs['item'] as $item){
                                    $article_ids[$item['article_id']] = $item['content']['news_item'][0]['title'] . '---' . $item['article_id'];
                                }
                            }
                        }
                    }
                $form->select('article_id')->rules('required_if:msgtype,mpnews') // 使用required_if
                ->setLabelClass(['asterisk'])->options($article_ids); // 显示 * 号;
            })->when(['wxcard'], function (Form $form){
                $form->text('card_id')->rules('required_if:msgtype,wxcard') // 使用required_if
                ->setLabelClass(['asterisk']); // 显示 * 号;
            });
            $form->text('media_id')->display(false);
            $form->text('thumb_media_id')->display(false);
            $form->text('hqmusicurl')->display(false);

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
